import {routerRedux} from 'dva/router';
import api from '../api';
import {message} from 'antd';

export default {
	namespace : 'benchmarkMgt',
	state:{
    benchmarkPage:{},
    currentpage:1,//当前页
    pagesize:15,//当前页数据条数
    sortField:'',//排序字段
    sortOrder:'',//排序类型
		benchmarksList:[],//基准归集
    benchmarkLists:[],//查询基准数据
    name:'',//查询name
    weight:[],
    benchmarkName:'',//复合基准名称
    benchmarkCode:'',//基准的公开代码
    benchmarkRate:'',//该基准占比
    baseRate:0,//绝对收益
	},
	subscriptions : {
    setup({dispatch, history}) {
      history.listen(({pathname, query}) => {
        // 没有参数时，向后端请求数据，并且改变query
        if (pathname === '/personalSettings/benchmarkMgt') {
          dispatch({type: 'init', payload: query})
        }
      })
    }
  },
  effects:{
  	* init({
				payload
			}, {call, put,select}) {
        // 初始化
				const state=yield select(state => state.dailyReplay)
				// 看本次的参数和上次的参数是否一致：
				// 保存参数到state
				const newObj={}
				for(var key in payload){
					newObj[key]=state[key]
				}
				if(!Object.getOwnPropertyNames(payload).length||JSON.stringify(payload)!=JSON.stringify(newObj)){
          yield put({
						type:'setState',
						payload
					})
				}
		},
		*getBenchmarks({payload},{call,put}){
			const query={};
      const res = yield call(api.benchmarkMgt.getBenchmarks, query)
      if (res.code === 0) {
        const benchmarksList = []
        const data = res.data;
        data.map((i)=>{
        	i.benchList.map((k)=>{
        		benchmarksList.push(k)
        	})
        })
        yield put({type: 'setState', payload: {
          benchmarksList,
        }})
      } else {
        message.info('获取基准数据失败！')
      }
		},
    *getBenchmark({payload},{call,put}){
      console.log(333,payload)
      const query={};
      payload.name&&(query.name=payload.name);
      payload.currentpage&&(query.currentPage=payload.currentpage);
      payload.pagesize&&(query.pageSize=payload.pagesize);
      const res = yield call(api.benchmarkMgt.getBenchmark, query)
      if (res.code === 0) {
        const benchmarkLists = res.data;
        const benchmarkPage = res.page;
        yield put({type: 'setState', payload: {
          benchmarkLists,
          benchmarkPage
        }})
      } else {
        message.info('获取基准数据失败！')
      }
    },
		*addBenchmark({payload},{call,put}){
			const query={};
      console.log(90000000,payload)
      for(var i = 0 ;i<payload.weight.length;i++) {
        if(payload.weight[i] == "" || typeof(payload.weight[i]) == "undefined"){
          payload.weight.splice(i,1);
          i= i-1;
        }
      }
      query.baseRate=payload.baseRate;
      query.isShare=parseInt(payload.isShare);
      payload.benchName&&(query.benchName=payload.benchName);
      payload.weight&&(query.weight=payload.weight);
      payload.description&&(query.description=payload.description);
      const res = yield call(api.benchmarkMgt.addBenchmark, query)
      yield put({
        type:'getBenchmark',
        payload:{
          currentpage:1,
          pagesize:15
        }
      })
		},
    *editBenchmark({payload},{call,put}){
      const query={};
      console.log(90000000,payload)
      for(var i = 0 ;i<payload.weight.length;i++) {
        if(payload.weight[i] == "" || typeof(payload.weight[i]) == "undefined"){
          payload.weight.splice(i,1);
          i= i-1;
        }
      }
      query.baseRate=payload.baseRate;
      query.isShare=parseInt(payload.isShare);
      payload.benchName&&(query.benchName=payload.benchName);
      payload.weight&&(query.weight=payload.weight);
      payload.benchId&&(query.benchId=payload.benchId);
      payload.description&&(query.description=payload.description);
       console.log(90000000,query)
      const res = yield call(api.benchmarkMgt.editBenchmark, query)
      console.log(55667711,res)
      yield put({
        type:'getBenchmark',
        payload:{
          currentpage:1,
          pagesize:15
        }
      })
    },
    *deleteBenchmark({payload},{call,put}){
      console.log(3333,payload)
      const benchId=payload.v
      const res = yield call(api.benchmarkMgt.deleteBenchmark,benchId );
      yield put({
        type:'getBenchmark',
        payload:{
          currentpage:1,
          pagesize:15
        }
      })
    },
    *changeRouter({payload:query},{call,put}){
      yield put(routerRedux.push({
        pathname:`/personalSettings/benchmarkMgt`,
        query:{
          ...query,
        }
      }))
    },
  },
  reducers : {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    }
  }
}